<?php include_template('header');?>
<table width="700" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="200" valign="top">
    <?php if(!is_login()){ ?>	
	<a href="<?php echo $config['site_url'];?>/signup.php">Join</a><br><a href="<?php echo $config['site_url'];?>/login.php">Login</a><br>
	<a href="<?php echo $config['site_url'];?>/signup.php">SubmitTopic</a><br>
	<?php }else{ ?>	
	<a href="<?php echo $config['site_url'];?>/editprofile.php">EditProfile</a><br>
	<a href="<?php echo $config['site_url'];?>/add_topic.php">SubmitTopic</a><br>
	<a href="<?php echo $config['site_url'];?>/logout.php">Logout</a><br>
	<?php } ?>
      <br>
	  <strong>Categories</strong><br>
      <br>
	  <a href="<?php echo $config['site_url'];?>">View All</a><br>
      <?php cat_list();?>
	  <br>
    <br></td>
    <td valign="top">
	<table width="100%" border="0" cellpadding="0" cellspacing="0">
	  <tr>
      <td width="25"><a href="<?php echo $config['site_url'];?>/xmlfeeds.php"><img src="<?php echo $config['site_url'];?>/templates/<?php echo $config['template'];?>/images/feed_icon.gif" width="20" height="20" border="0"></a></td>
      <td><span class="pagetitle"><?php echo page_title('cat');?></span></td>
	  <td width="350">
	  <div id="tabsB">
	  <ul>
</ul>
</div>
	  </td>
	  </tr>
	  <tr><td height="1" bgcolor="#dedede"></td><td height="1" bgcolor="#dedede"></td><td height="1" bgcolor="#dedede"></td></tr>
	  </table>
	<br>
    <br>
	<?php 
	if(!isset($_GET['page']) || !ctype_digit($_GET['page'])){
		$page = 1;
	}else{
		$page = $_GET['page'];
	}
	include_logic('topic');
	include_logic('user');
	include_logic('cat');
	$cursor = topic_find();
	$total = $cursor->count();
	if($total == 0){
	?>
	No Topics Available
	<?php
	}else{
	$total_pages = ceil($total/$config['perpage']);
	if($page>$total_pages)
	{
		$page = $total_pages;
	}
	$topics = $cursor->sort(array('score' =>-1))->skip(($page-1)*$config['perpage'])->limit($config['perpage']);
	foreach($topics as $topic){
		$user = user_find_one(array('_id'=>new MongoId($topic['user_id'])));
		$cat = cat_find_one(array('_id'=>new MongoId($topic['cat_id'])));
		$comment_count = count($topic['comment']);
	?>
	<table width="100%" cellpadding="0" cellspacing="0" style="padding-bottom:20px;">
	<tr><td width="65" align="left" valign="top"><table border="0" cellspacing="0" cellpadding="0" class="digbox"><tr><td><table width="52" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td align="center" height="28" valign="bottom"><div class="digs" id="digs_<?php echo $topic['_id'];?>"><?php echo $topic['score'];?></div></td>
      </tr>
	  <tr>
	  <td height="24" align="center" valign="middle"><span class="digg">digs</span></td>
	  </tr>
      <tr>
        <td height="2"></td>
      </tr>
      <tr>
           <td height="25" align="center"><div class="digit" id="digbut"><a href="javascript:void(0);" onclick="digit('<?php echo $topic['_id'];?>');">dig it</a></div></td>
      </tr>
    </table></td></tr></table></td>
	<td valign="top">
	<div>
	  <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td valign="top"><a href="<?php echo $topic['url'];?>"><?php echo $topic['title'];?></a></td>
        </tr>
		<tr>
		<td height="20"><table border="0" cellpadding="0" cellspacing="0">
		  <tr><td width="21"><img src="<?php echo $config['site_url'];?>/templates/<?php echo $config['template'];?>/images/noavatar16x16.gif"></td>
		  <td><span class="subline">submitted by  <a href="#"><?php echo $user['username'];?></a>  <?php echo time_taken(time()-$topic['entry_time']);?> ago</span></td></tr></table></td>
		</tr>
        <tr>
          <td valign="top"><?php echo $topic['description'];?></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td valign="bottom"><span class="bottomlinks"><a href="<?php echo $config['site_url'];?>/topic.php?id=<?php echo $topic['_id'];?>#comments"><?php echo $comment_count ;?> Comments</a> - <a href="<?php echo $config['site_url'];?>/topic.php?id=<?php echo $topic['_id'];?>">MoreInfo</a> - <a href="javascript:void(0);" onclick="buryit('<?php echo $topic['_id'];?>');">Bury</a> &nbsp; Categories: <a href="<?php echo $config['site_url'];?>/cat.php?cat_id=<?php echo $topic['cat_id'];?>"><?php echo $cat['name'];?></a></span></td>
        </tr>
      </table></div></td>
	  </tr>
	</table>
    <?
	}
	?>
	<br><br>
    <div id="pages">
        <ul>
		<?php 
		    $pagearr = pagenav($total,$page,$config['perpage'],$config['site_url'].'/index.php',$posts=0);
			foreach($pagearr as $pages){
			if($pages['current']==1){
		?>  <li class="current"><?php echo $pages['title'];?></li>
		<?php } else {?>
			<li><a href="<?php echo $pages['link'];?>"><?php echo $pages['title'];?></a></li>
		<?php } 
		}?>	
        </ul>
    </div>
	<br>  
    <?php } ?>	
    </td>
  </tr>
</table>
<?php include_template('footer');?>